package com.galleryviewpager.d_chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.galleryviewpager.DensityUtils;

import java.util.ArrayList;


//竖向数据
public class TestSimpleLineView extends View {

    private Context mContext;

    private int mWidth, mHeight;

    private Paint mPaint = new Paint();

    private final String[] moneyTags = {"10k", "20k", "30k", "40k", "50k"};

    private final String[] monthTags = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月"};

    private ArrayList<Integer> positionX = new ArrayList<>();

    private ArrayList<Integer> positionY = new ArrayList<>();


    public TestSimpleLineView(Context context) {
        this(context, null);
    }

    public TestSimpleLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestSimpleLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(20f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = DensityUtils.dip2px(mContext, 500);
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = DensityUtils.dip2px(mContext, 500);
        }

        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        Log.e(this.getClass().getSimpleName(), " top: " + fontMetrics.top + " descent: " + fontMetrics.descent + " ascent: " + fontMetrics.ascent + " bottom: " + fontMetrics.bottom);
        float wordHeight = fontMetrics.descent - fontMetrics.ascent;

        float distance = mHeight / moneyTags.length;

        for (int i = 0; i < moneyTags.length; i++) {
            String moneyTag = moneyTags[i];
            canvas.drawText(moneyTag, 0, distance * i + wordHeight, mPaint);
            positionY.add((int) (distance * i + wordHeight));
        }
        float v = mPaint.measureText(moneyTags[0]);
        canvas.drawLine(v, 0, v, mHeight, mPaint);

        int widthDistance = mWidth / monthTags.length;
        float measureText = mPaint.measureText(moneyTags[moneyTags.length - 1]);
        for (int i = 0; i < monthTags.length; i++) {
            String monthTag = monthTags[i];
            float textLength = mPaint.measureText(monthTag);
            canvas.drawText(monthTag, widthDistance * i + measureText, mHeight, mPaint);
            positionX.add((int) (widthDistance * i + measureText));
        }

        canvas.drawLine(0, mHeight - wordHeight, mWidth, mHeight - wordHeight, mPaint);

        mPaint.setColor(Color.RED);
        int preX = 0, preY = 0;
        for (int i = 0; i < 8; i++) {
            int vY = (int) (Math.random() * 4);
            canvas.drawCircle(positionX.get(i), positionY.get(vY), 6f, mPaint);
            if (i >= 1) {
                canvas.drawLine(positionX.get(preX), positionY.get(preY), positionX.get(i), positionY.get(vY), mPaint);
            }
            preX = i;
            preY = vY;

        }

    }
}
